package backtracking.leetcode.medium;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bruin_du
 * @description 没有重复元素集合的全排列
 * @date 2022/9/6 23:49
 **/
public class OfferII083_Permute {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        int n = nums.length;
        boolean[] visited = new boolean[n];
        dfs(nums, n, visited);
        return lists;
    }

    private void dfs(int[] nums, int n, boolean[] visited) {
        if (list.size() == n) {
            lists.add(new ArrayList<Integer>(list));
            return;
        }
        for (int i = 0; i < n; i++) {
            if (visited[i]) continue;
            list.add(nums[i]);
            visited[i] = true;
            dfs(nums, n, visited);
            list.remove(list.size() - 1);
            visited[i] = false;
        }
    }
}
